package flc.ast.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.List;

import flc.ast.AppConfig;
import flc.ast.R;
import flc.ast.activity.PizzaDetailActivity;
import flc.ast.adapter.PizzaAdapter;
import flc.ast.databinding.FragmentHomeBinding;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.FastClickUtil;
import stark.common.bean.StkResBean;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HomeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HomeFragment extends BaseNoModelFragment<FragmentHomeBinding> {

    private PizzaAdapter pizzaAdapter;
    private StkResBean stkResBean;
    private int oldPosition;

    @Override
    protected int onCreate() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);
        mDataBinding.ivLook.setOnClickListener(this);
        mDataBinding.rlPizzaName.setOnClickListener(this);
        mDataBinding.rvPizzaList.setLayoutManager(new LinearLayoutManager(mContext,LinearLayoutManager.HORIZONTAL,false));
        pizzaAdapter=new PizzaAdapter();
        mDataBinding.rvPizzaList.setAdapter(pizzaAdapter);
        pizzaAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        if (FastClickUtil.isFastClick()) return;
        switch (v.getId()){
            case R.id.ivLook:
            case R.id.rlPizzaName:
                PizzaDetailActivity.stkResBean=stkResBean;
                startActivity(PizzaDetailActivity.class);
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        pizzaAdapter.getItem(oldPosition).setSelected(false);
        pizzaAdapter.notifyItemChanged(oldPosition);
        stkResBean=pizzaAdapter.getItem(position);
        oldPosition=position;
        pizzaAdapter.getItem(position).setSelected(true);
        pizzaAdapter.notifyItemChanged(position);
        setSelData();
    }

    @Override
    protected void initData() {
        getData();
    }

    private void getData(){
        StkResApi.getTagResourceList(this, AppConfig.PIZZA_DATA, StkResApi.createParamMap(0, 10), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success){
                    if (result != null && result.size() > 0) {
                        stkResBean=result.get(0);
                        result.get(0).setSelected(true);
                        pizzaAdapter.setList(result);
                        setSelData();
                    }
                }
            }
        });
    }

    private void setSelData(){
        Glide.with(mContext).load(stkResBean.getThumbUrl()).into(mDataBinding.ivPizzaImg);
        mDataBinding.tvPizzaName.setText(stkResBean.getName());
        mDataBinding.tvPizzaDosing.setText(stkResBean.getDesc());
    }

}